<template>
  <el-container>
    <el-aside width="250px">
      <DatabaseInfo
        @open="handleOpen"
        @close="handleClose"
        @select="handleSelect"
        :dbInfo="props.dbInfo"
				:database-info="props.databaseInfo"
      ></DatabaseInfo>
    </el-aside>
    <el-main style="padding: 12px">
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane :label="`已选择数据库：${currentDb}`" name="tableInfo">
          <!--超表列表详情-->
          <el-form :inline="true">
            <el-form-item label="">
              <el-input size="small" placeholder="请输入表名" v-model="searchWord" clearable />
            </el-form-item>
            <el-form-item label="">
              <el-input size="small" placeholder="请输入场站" v-model="searchTn" clearable />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchConidtionST()" class="el-icon-search" :icon="Search" size="small"
                >查询</el-button
              >
              <el-button type="default" @click="addTable" class="el-icon-plus" size="small"
                ><el-icon><Plus /></el-icon>新增</el-button
              >
              <el-button type="default" @click="addSTable" class="el-icon-plus" size="small" :icon="Share"
                >初始化超表</el-button
              >
              <el-button v-if="false" type="default" @click="initTi" class="el-icon-plus" size="small" :icon="Share"
                >初始化ti</el-button
              >
              <el-button v-if="false" type="default" @click="convertTn" class="el-icon-plus" size="small" :icon="Share"
                >初始化tn</el-button
              >
              <el-button v-if="true" type="default" @click="initTn" size="small" :icon="Share">修改测点tn</el-button>
              <el-button type="default" @click="batchAddChildTable()" class="el-icon-plus" size="small"
                >导入表<el-icon class="el-icon--right"><Upload /></el-icon
              ></el-button>
              <el-button type="default" @click="batchDeleteChildTable()" class="el-icon-plus" size="small"
                >批量删除<el-icon class="el-icon--right"><Upload /></el-icon
              ></el-button>
              <el-button
                v-if="false"
                type="default"
                @click="batchUpdateChildTableDesc()"
                class="el-icon-plus"
                size="small"
                >修改描述信息<el-icon class="el-icon--right"><Upload /></el-icon
              ></el-button>
              <el-button type="default" @click="download()" class="el-icon-plus" size="small"
                >模板下载<el-icon class="el-icon--right"><Download /></el-icon
              ></el-button>
              <el-dropdown size="small" class="right" @command="handleCommand">
                <el-button type="default" size="small">
                  导出<el-icon class="el-icon--right"><arrow-down /></el-icon>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="search">查询结果</el-dropdown-item>
                    <el-dropdown-item command="all">全部</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
              <!-- <el-button type="success" @click="syncData" size="small" :icon="Link">同步数据</el-button> -->
            </el-form-item>
          </el-form>
          <!-- <el-table-v2 :columns="stableColumns" :data="stData" :width="dialogWidth" :height="height" fixed /> -->
          <el-table
            style="height: calc(100% - 132px)"
            v-loading="loading"
            border
            :data="stData"
            :width="dialogWidth"
            @selection-change="handleSelectionChange"
          >
            <template #empty>
              <el-empty />
            </template>
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column prop="tagName" label="表名" width="200"> </el-table-column>
            <el-table-column prop="tagDesc" label="描述" width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="ti" label="ti" width="200" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="tn" label="tn" width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="ta" label="ta" width="50" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="tagType" label="类型" width="150"> </el-table-column>

            <el-table-column
              prop="tagStatue"
              label="状态"
              width="100"
              :filters="[
                { text: '未同步', value: '未同步' },
                { text: '已同步', value: '已同步' },
              ]"
              filter-placement="bottom-end"
            >
              <template v-slot="scope">
                <el-tag :type="scope.row.tagStatue === '未同步' ? 'primary' : 'success'" disable-transitions>{{
                  scope.row.tagStatue
                }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" minWidth="300">
              <template v-slot="scope">
                <!-- <el-button type="primary" plain @click="btn_showTables(scope.row)">查看子表</el-button> -->
                <el-button type="primary" plain @click="btn_editTables(scope.row)">编辑</el-button>
                <el-button
                  type="danger"
                  :disabled="scope.row.tagStatue == '已同步'"
                  plain
                  @click="btn_deleteTables(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div style="margin-top: 8px">
            <el-button :disabled="true" style="float: left" type="default" plain @click="btn_batchDeleteTables()"
              >批量删除</el-button
            >
            <el-pagination
              style="float: right"
              v-model:current-page="currentPage"
              v-model:page-size="pageSize"
              :page-sizes="[10, 100, 200, 400]"
              :size="size"
              :disabled="disabled"
              :background="background"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-main>
    <el-dialog :title="getDialogTitle()" v-model="uploadDialogVisible" width="60%" style="height: 380px">
      <el-upload
        ref="upload"
        class="upload-demo"
        :action="actionUrl"
        :limit="1"
        :on-exceed="handleExceed"
        :auto-upload="false"
        :before-upload="handleBeforeUpload"
      >
        <template #trigger>
          <el-button type="primary">选择文件</el-button>
        </template>
        <el-button class="ml-3" type="success" @click="submitUpload"> 上传到服务器 </el-button>
        <template #tip>
          <div class="el-upload__tip text-red">限制文件数量为1, 新文件会覆盖旧文件</div>
        </template>
      </el-upload>
    </el-dialog>

    <el-dialog title="$t('表信息')" v-model="editDialogVisible" width="40%" style="height: 520px">
      <el-form ref="editForm" :model="editform" label-width="80px">
        <el-form-item label="表类型">
          <!-- <el-input :disabled="editDisabled" v-model="editform.tagType"></el-input> -->
          <el-select v-model="editform.tagType" placeholder="请选择" size="large" style="width: 240px">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="表名">
          <el-input :disabled="editDisabled" v-model="editform.tagName"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="editform.tagDesc"></el-input>
        </el-form-item>
        <el-form-item label="ti">
          <el-input :disabled="true" v-model="editform.ti"></el-input>
        </el-form-item>
        <el-form-item label="tn">
          <el-input v-model="editform.tn"></el-input>
        </el-form-item>
        <el-form-item label="ta">
          <el-input v-model="editform.ta"></el-input>
        </el-form-item>
        <el-row>
          <el-col span="6">
            <el-form-item label="小数位数">
              <el-input-number
                v-model="editform.pointNum"
                controls-position="right"
                :min="0"
                :max="6"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col span="6">
            <el-form-item label="单位">
              <el-input v-model="editform.unit" style="width: 60%"></el-input>
            </el-form-item>
          </el-col>
          <el-col span="6">
            <el-form-item label="量程上限">
              <el-input-number
                v-model="editform.maxValue"
                :precision="2"
                controls-position="right"
                :min="0.0"
                :max="100.0"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col span="6">
            <el-form-item label="量程下限">
              <el-input-number
                v-model="editform.minValue"
                :precision="2"
                controls-position="right"
                :min="0.0"
                :max="100.0"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12"> </el-col>
          <el-col :span="12">
            <el-button style="float: right; margin-left: 10px" @click="editDialogVisible = false">关闭</el-button>
            <el-button style="float: right" type="primary" @click="submitEditData">确定</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </el-container>
</template>

<script lang="tsx" setup>
import { ref, toRaw, watch, defineProps } from "vue";
import { onMounted } from "vue";
import { ArrowDown } from "@element-plus/icons-vue";
import { Delete, Edit, Search, Share, Upload, Download, Link } from "@element-plus/icons-vue";
import {
  ElMessage,
  ElButton,
  TableV2FixedDir,
  ElTooltip,
  ElIcon,
  ElTag,
  ComponentSize,
  ElMessageBox,
} from "element-plus";
import {
  executeSQL,
  showSTables,
  showTablesBySTable,
  addSuperTable,
  initTiTable,
  initTnTable,
  convertTnTable,
} from "@/api/index";
import { showTables, deleteTables, syncDBToTDengine, updateTagDesc } from "@/api/dbsmanagepoints";
import { submitTableData } from "@/api/collectorManage";
import { defaultAxios } from "@/utils/http/index.js";

//属性值
const props = defineProps({
  dbInfo: {
    type: Object,
    default: {},
  },
	databaseInfo:{
		type: Array,
		default: []
	}
});
// import TableDialog from "./TableDialog.vue"

//table还是stable
const tableType = ref("table");
const dialogVisible = ref(false);
const editDialogVisible = ref(false);
const editDisabled = ref(true);
const options = [
  {
    value: "FLOAT",
    label: "FLOAT",
  },
  {
    value: "DOUBLE",
    label: "DOUBLE",
  },
  {
    value: "INT",
    label: "INT",
  },
  {
    value: "BOOL",
    label: "BOOL",
  },
];

const uploadDialogVisible = ref(false);
import { genFileId } from "element-plus";
import type { UploadInstance, UploadProps, UploadRawFile } from "element-plus";
import axios from "axios";
import DatabaseInfo from "../../components/DatabaseInfo.vue";
const upload = ref<UploadInstance>();
const actionUrl = ref("");
const updateTagDescUrl = ref("");
const updateTagTnUrl = ref("");

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);
const multipleSelection = ref([]);

//导入子表1 2删除子表  3修改测点描述信息   4修改tn信息
const dialogType = ref(1);

const loading = ref(false);
const height = ref();
const tableWidth = ref(0);
const dialogWidth = ref(0);
const store = {
  info: props.dbInfo,
	databaseInfo:props.databaseInfo
};
const currentDb = ref("");
const currentStable = ref("");

//数据

//超表搜索条件
const searchWord = ref("");
const searchTn = ref("");
//超表数据
const stData = ref([]);

const editform = ref({
  tagName: "",
  ti: "",
  tn: "",
  ta: -1,
  tagType: "",
  tagDesc: "",
  pointNum: 2,
  unit: "",
  maxValue: 100.0,
  minValue: 0.0,
});

//超表结构数据
const stableColumns = ref([
  {
    key: "tagName",
    title: "表名",
    dataKey: "tagName",
    width: 300,
    align: "center",
    cellRenderer: ({ cellData: tagName }) => <ElTag>{tagName} </ElTag>,
  },
  {
    key: "tagDesc",
    title: "描述",
    dataKey: "tagDesc",
    width: 300,
    align: "center",
    cellRenderer: ({ cellData: tagDesc }) => <ElTag>{tagDesc} </ElTag>,
  },
  {
    key: "tagType",
    title: "类型",
    dataKey: "tagType",
    width: 300,
    align: "center",
    cellRenderer: ({ cellData: tagType }) => <ElTag>{tagType} </ElTag>,
  },
  {
    key: "operations",
    title: "操作",
    dataKey: "table_name",
    cellRenderer: (cellData: any) => (
      <>
        <ElButton
          size="small"
          onClick={() => {
            btn_showTables(cellData.rowData);
          }}
        >
          查看子表
        </ElButton>
      </>
    ),
    width: 300,
    align: "center",
  },
]);
//在操作列添加按钮操作
// <ElButton size="small" > 编辑 </ElButton>
// <ElButton size="small" type="danger" >删除</ElButton>

//表数据
const tData = ref([]);

//tab页
const activeName = ref("tableInfo");

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
  currentDb.value = key;
};

const getDialogTitle = () => {
  if (dialogType.value == 1) {
    return "导入测点";
  } else if (dialogType.value == 2) {
    return "删除测点";
  } else if (dialogType.value == 3) {
    return "修改测点描述";
  } else if (dialogType.value == 4) {
    return "修改测点tn";
  } else {
    return "未知";
  }
};

/**
 * 初始化超级表
 */
const addSTable = (key: string, keyPath: string[]) => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库再进行初始化超表",
      type: "error",
    });
    return;
  }
  //初始化超级表需要的参数
  let payload = {
    ...store.info,
    dbName: currentDb.value,
  };

  addSuperTable(payload)
    .then((r) => {
      ElMessage({
        message: "初始化超级表成功",
        type: "success",
      });
    })
    .catch((err) => {
      ElMessage({
        message: "初始化超级表失败",
        type: "error",
      });
    });
};
/**
 *
 * @param key 初始化ti
 * @param keyPath
 */
const initTi = (key: string, keyPath: string[]) => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库再进行初始化ti",
      type: "error",
    });
    return;
  }
  loading.value = true;
  //初始化超级表需要的参数
  let payload = {
    ...store.info,
    dbName: currentDb.value,
  };

  initTiTable(payload)
    .then((r) => {
      ElMessage({
        message: "初始化ti成功",
        type: "success",
      });
      loading.value = false;
    })
    .catch((err) => {
      ElMessage({
        message: "初始化ti失败",
        type: "error",
      });
      loading.value = false;
    });
};
/**
 *
 * @param key 初始化tn
 * @param keyPath
 */
const convertTn = (key: string, keyPath: string[]) => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库再进行初始tn",
      type: "error",
    });
    return;
  }
  ElMessageBox.confirm("请确认是否同步所有记录的TN字段?")
    .then(() => {
      loading.value = true;
      //初始化超级表需要的参数
      let payload = {
        ...store.info,
        dbName: currentDb.value,
      };

      convertTnTable(payload)
        .then((r) => {
          ElMessage({
            message: "初始化tn成功",
            type: "success",
          });
          loading.value = false;
        })
        .catch((err) => {
          ElMessage({
            message: "初始化tn失败",
            type: "error",
          });
          loading.value = false;
        });
    })
    .catch(() => {
      ElMessage({
        message: "已取消",
        type: "success",
      });
    });
};

/**
 *
 * @param key 初始化tn
 * @param keyPath
 */
const initTn = (key: string, keyPath: string[]) => {
  // if(currentDb.value == null || currentDb.value == ''){
  //   ElMessage({
  //           message: '请先选择数据库再进行初始化超表',
  //           type: 'error',
  //         });
  //   return;
  // }
  // loading.value= true;
  // //初始化超级表需要的参数
  // let payload = {
  //   ...store.info,
  //   dbName: currentDb.value
  // };

  // initTnTable(payload).then(r => {
  //   ElMessage({
  //     message: '初始化tn成功',
  //     type: 'success',
  //   });
  //   loading.value= false;
  // }).catch(err => {
  //   ElMessage({
  //     message: '初始化tn失败',
  //     type: 'error',
  //   });
  //   loading.value= false;
  // });
  dialogType.value = 4;
  uploadDialogVisible.value = true;
};

const syncData = (key: string, keyPath: string[]) => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库",
      type: "error",
    });
    return;
  }
  ElMessageBox.confirm("请确认是否同步当前查询条件下的所有记录?")
    .then(() => {
      let payload = {
        ...store.info,
        dbName: currentDb.value,
      };
      let param = {
        dbConfig: payload,
        tableName: searchWord.value,
        tn:searchTn.value,
      };
      loading.value = true;
      syncDBToTDengine(param)
        .then((r) => {
          ElMessage({
            message: "同步完成",
            type: "success",
          });
          loading.value = false;
          searchConidtionST();
        })
        .catch((err) => {
          ElMessage({
            message: "同步数据失败",
            type: "error",
          });
          loading.value = false;
        });
    })
    .catch(() => {
      ElMessage({
        message: "已取消",
        type: "success",
      });
    });
};

//选择数据库
const handleSelect = (key: string, keyPath: string[]) => {
  currentDb.value = key;
  searchWord.value = "";
  searchTn.value = "";
  /**
   * 获取table列表
   */
  let payload = {
    ...store.info,
    dbName: currentDb.value,
  };
  let param = {
    dbConfig: payload,
    tableName: searchWord.value,
    tn:searchTn.value,
    pageSize: pageSize.value,
    currentPage: currentPage.value,
  };
  stData.value = [];
  total.value = 0;
  /*
  loading.value = true;
  showTables(param)
    .then((r) => {
      stData.value = r.data.records.map((item: any, index: number) => {
        return item;
      });
      total.value = r.data.total;
      loading.value = false;
    })
    .catch((err) => {
      ElMessage({
        message: "查询数据失败",
        type: "error",
      });
      loading.value = false;
    });
    */
};

const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};

/**
 * 从超表跳转到子表列表
 * @param cellData
 */
const btn_showTables = (cellData) => {
  dialogVisible.value = true;
  let name = cellData.table_name;
  currentStable.value = name;
};
const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};
/**
 * 新增操作
 */
const addTable = () => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库再进行初始化超表",
      type: "error",
    });
    return;
  }
  editDialogVisible.value = true;
  editDisabled.value = false;
  editform.value = {
    tagName: "",
    ti: "",
    tn: "",
    ta: -1,
    tagType: "FLOAT",
    tagDesc: "",
    pointNum: 2,
    unit: "",
    maxValue: 100.0,
    minValue: 0.0,
  };
};
/**
 * 编辑操作
 */
const btn_editTables = (cellData) => {
  editDialogVisible.value = true;
  editDisabled.value = true;
  editform.value = cellData;
};
/**
 * 编辑弹窗提交
 */
const submitEditData = () => {
  ElMessageBox.confirm("请确认是否提交数据?")
    .then(() => {
      submitData();
    })
    .catch(() => {
      ElMessage({
        message: "已取消",
        type: "success",
      });
    });
};

const submitData = () => {
  /**
   * 获取table列表
   */
  let payload = {
    ...store.info,
    dbName: currentDb.value,
  };
  let param = {
    dbConfig: payload,
    tagInfo: editform.value,
  };
  loading.value = true;
  submitTableData(param)
    .then((r) => {
      loading.value = false;
      ElMessage({
        message: "提交数据数据成功",
        type: "success",
      });
      editDialogVisible.value = false;
    })
    .catch((err) => {
      ElMessage({
        message: "提交数据数据失败",
        type: "error",
      });
      loading.value = false;
    });
};
/**
 * 删除操作
 */
const btn_deleteTables = (cellData) => {
  ElMessageBox.confirm("请确认是否删除该表记录?")
    .then(() => {
      let params = [];
      params.push(cellData);
      deleteTables(params).then((r) => {
        searchConidtionST();
      });
    })
    .catch(() => {
      ElMessage({
        message: "已取消",
        type: "success",
      });
    });
};
/**
 * 批量删除操作
 */
const btn_batchDeleteTables = (cellData) => {
  ElMessageBox.confirm("请确认是否删除该表记录?")
    .then(() => {
      deleteTables(multipleSelection.value).then((r) => {
        searchConidtionST();
      });
    })
    .catch(() => {
      ElMessage({
        message: "已取消",
        type: "success",
      });
    });
};

onMounted(() => {
  actionUrl.value = defaultAxios.getBaseUrl() + "/api/tdengine/managerpoints/uploadFile";
  updateTagDescUrl.value = defaultAxios.getBaseUrl() + "/api/tdengine/managerpoints/updateTagDescByFile";
  updateTagTnUrl.value = defaultAxios.getBaseUrl() + "/api/tdengine/managerpoints/updateTagTnByFile";
  setTimeout(() => {
    init();
  }, 100);
});

watch(
  (deep) => store.info,
  (val) => {
    if (val && store.connected) {
      init();
    }
  }
);

const init = () => {
  height.value = document.documentElement.clientHeight - 30;
  dialogWidth.value = document.documentElement.clientWidth * 0.8 - 10;
};

const searchConidtionST = () => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库",
      type: "error",
    });
    return;
  }
  /**
   * 获取table列表
   */
  let payload = {
    ...store.info,
    dbName: currentDb.value,
  };
  let param = {
    dbConfig: payload,
    tableName: searchWord.value,
    tn: searchTn.value,
    pageSize: pageSize.value,
    currentPage: currentPage.value,
  };
  loading.value = true;
  showTables(param)
    .then((r) => {
      stData.value = r.data.records.map((item: any, index: number) => {
        return item;
      });
      total.value = r.data.total;
      loading.value = false;
    })
    .catch((err) => {
      ElMessage({
        message: "查询数据失败",
        type: "error",
      });
      loading.value = false;
    });
};

const handleClick = (tab: any) => {
  console.log(tab);
};

const download = () => {
  //导入测点表的模板下载
  let url = "子表导入模板.xlsx";
  let a = document.createElement("a");
  document.body.appendChild(a);
  var fullUrl = window.location.href;
  if (fullUrl.indexOf("dist") > -1) {
    a.href = "/dist/static/file/" + url;
  } else {
    a.href = "/static/file/" + url;
  }
  a.download = url;
  a.style.display = "none";
  a.click();
  document.body.removeChild(a);
};

const handleExceed: UploadProps["onExceed"] = (files) => {
  upload.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  upload.value!.handleStart(file);
};

const submitUpload = () => {
  if (dialogType != 2) {
    upload.value!.submit();
  }
  uploadDialogVisible.value = false;
};

const updateTagDescByFile = (formData) => {
  loading.value = true;
  updateTagDesc(formData)
    .then((res) => {
      console.log(res);
      loading.value = false;
    })
    .catch((e) => {
      console.error(e);
      loading.value = false;
    });
};

const handleBeforeUpload = async (file) => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库",
      type: "error",
    });
    return;
  }
  // 使用axios发送文件前，确保创建FormData实例
  const formData = new FormData();
  formData.append("file", file);
  formData.append("stable", "");
  let payload = {
    ...store.info,
    dbName: currentDb.value,
  };
  formData.append("host", payload.host);
  formData.append("port", payload.port);
  formData.append("username", payload.username);
  formData.append("password", payload.password);
  formData.append("dbName", currentDb.value);
  formData.append("type", dialogType.value == 1);
  loading.value = true;
  // 配置axios请求
  axios({
    method: "post",
    url:
      dialogType.value == 4 ? updateTagTnUrl.value : dialogType.value == 3 ? updateTagDescUrl.value : actionUrl.value,
    // 上传数据直接为formData实例即可，无需通过JSON封装，否则无法解析！！！
    data: formData,
    headers: { "Content-Type": "multipart/form-data" },
  })
    .then((response) => {
      loading.value = false;
      if (response.data.code == 200) {
        searchConidtionST();
        ElMessage({
          message: "执行成功",
          type: "success",
        });
      } else {
        ElMessage({
          message: response.data.msg,
          type: "error",
        });
      }
    })
    .catch((error) => {
      loading.value = false;
      ElMessage({
        message: "执行失败",
        type: "error",
      });
    });
  return false;
};

const handleSizeChange = (val: number) => {
  searchConidtionST();
};
const handleCurrentChange = (val: number) => {
  searchConidtionST();
};

const handleCommand = (command: string | number | object) => {
  if (currentDb.value == null || currentDb.value == "") {
    ElMessage({
      message: "请先选择数据库",
      type: "error",
    });
    return;
  }
  exportExcel(command);
};
const exportExcel = async (commmand) => {
  //?command='+commmand+'&filter'+searchWord.value
  loading.value = true;
  try {
    let payload = {
      ...store.info,
      dbName: currentDb.value,
    };
    let data = {
      dbConfig: payload,
      tableName: searchWord.value,
      tn: searchTn.value,
      command: commmand,
    };
    const response = await axios({
      url: defaultAxios.getBaseUrl() + "/api/tdengine/managerpoints/export", // 后端API端点
      method: "POST",
      data,
      responseType: "blob", // 设置响应类型为二进制
    });
    loading.value = false;
    const blob = new Blob([response.data], { type: "application/vnd.ms-excel" });
    const link = document.createElement("a");
    link.href = URL.createObjectURL(blob);
    link.download = "data.xlsx"; // 导出文件名
    link.click();
  } catch (error) {
    console.error("导出失败:", error);
  }
};

const batchAddChildTable = () => {
  dialogType.value = 1;
  uploadDialogVisible.value = true;
};

const batchDeleteChildTable = () => {
  dialogType.value = 2;
  uploadDialogVisible.value = true;
};

const batchUpdateChildTableDesc = () => {
  dialogType.value = 3;
  uploadDialogVisible.value = true;
};

// const columns = generateColumns(10)
// const data = generateData(columns, 100000)
</script>

<style scoped>
/* 添加一些样式以适应你的需求 */
.ml-3 {
  margin-left: 20px;
}
.demo-tabs {
  width: 100%;
  height: 100%;
}
.el-sub-menu.is-active {
  background-color: #a2d2ff;
}

/* 覆盖菜单激活项的背景颜色 */
.el-menu-item.is-active {
  color: hsl(246, 100%, 54%) !important;
  /* 选中项的文本颜色 */
}

.pcolor {
  background-color: #051422;
}

.right {
  padding-left: 20px;
  line-height: 30px;
  float: right;
}

.codemirror {
  width: 100%;
  height: 300px;
}

.monaco-editor {
  width: 100%;
  height: 100%;
}
</style>